package asf.forms
{
	import asf.interfaces.IFormField;
	import asf.interfaces.IRadioGroupItem;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.getDefinitionByName;

	public class RadioGroup extends Sprite implements IFormField
	{
		private var items:Array;
		private var dict:Object;
		
		private var raw:XML;
		private var selectedItem:IRadioGroupItem;
		
		public function RadioGroup( p_items:XML )
		{
			super( );
			raw = p_items;
			parse( );
		}
		
		private function parse( ):void
		{
			var itemXML:XML;
			var item:DisplayObject;
			var ItemClass:Class;
			
			items = new Array( );
			dict = new Object( );
			
			for each( itemXML in raw.item )
			{
				ItemClass = getDefinitionByName( itemXML.@baseClass || itemXML.baseClass ) as Class;
				item = new ItemClass( );
				
				item.x = Number( itemXML.@x || itemXML.x );
				item.y = Number( itemXML.@y || itemXML.y );
				
				( item as IRadioGroupItem ).setChecked( itemXML.@checked == "true" || itemXML.checked == "true" );
				( item as IRadioGroupItem ).setValue( String( itemXML.@value || itemXML.value ) );
				
				item.addEventListener( MouseEvent.CLICK, itemClickHandler, false, 0, true );
				
				items.push( item );
				dict[ itemXML.@id || itemXML.id ] = item;
				
				addChild( item );
			}
		}
		
		private function itemClickHandler( evt:MouseEvent ):void
		{
			var target:IRadioGroupItem = evt.currentTarget as IRadioGroupItem;
			checkItem( target );
		}
		
		public function checkItemByID( itemID:String ):void
		{
			var item:IRadioGroupItem = dict[ itemID ];
			checkItem( item );
		}
		
		private function checkItem( item:IRadioGroupItem ):void
		{
			if( selectedItem )
			{
				selectedItem.setChecked( false );
			}
			
			selectedItem = item;
			item.setChecked( true );
		}
		
		public function dispose( ):void
		{
			var item:IRadioGroupItem;
			
			for each( item in items )
			{
				( item as DisplayObject ).removeEventListener( MouseEvent.CLICK, itemClickHandler );
				
				item.dispose( );
				removeChild( item as DisplayObject );
				
				item = null;
			}
			
			items = null;
			dict = null;
		}
		
		public function setValue( value:* ):void
		{
			var item:IRadioGroupItem;
			
			for each( item in items )
			{
				if( item.getValue( ) == value )
				{
					checkItem( item );
					return;
				}
			}
		}
		
		public function getValue( ):*
		{
			if( selectedItem )
			{
				return selectedItem.getValue( );
			}
			
			return null;
		}
		
		public function clearValue( ):void
		{
			if( selectedItem )
			{
				selectedItem.setChecked( false );
			}
			
			selectedItem = null;
		}
		
		public function focus( ):void
		{
			if( items.length > 0 )
			{
				( items[ 0 ] as IRadioGroupItem ).focus( );
			}
		}
		
		public function blur( ):void
		{
			var item:IRadioGroupItem;
			for each( item in items ) item.blur( );
		}
		
		public function setWidth(value:Number):void
		{
		}
		
		public function setHeight(value:Number):void
		{
		}
		
		public function getWidth( ):Number
		{
			return 0;
		}
		
		public function getHeight( ):Number
		{
			return 0;
		}
		
		public function setTabIndex( value:int ):void
		{
		}
		
		public function getTabIndex( ):int
		{
			return 0;
		}
		
		public function enable( ):void
		{
			var item:IRadioGroupItem;
			
			for each( item in items ) item.enable( );
		}
		
		public function disable( ):void
		{
			var item:IRadioGroupItem;
			
			for each( item in items ) item.disable( );
		}
		
		public function isValid( ):Boolean
		{
			return true;
		}
		
		public function isEmpty( ):Boolean
		{
			return selectedItem == null;
		}
		
		public function markError( message:String ):void
		{
			var item:IRadioGroupItem;
			
			for each( item in items ) item.markError( message );
		}
		
		public function clearError( ):void
		{
			var item:IRadioGroupItem;
			
			for each( item in items ) item.clearError( );
		}
	}
}